#include "dd_line_2d.h"

#include "util/mmgr.h"		// Needs to come in early

using namespace dd;

Line2D::Line2D()
{
}

Line2D::~Line2D()
{

}

void Line2D::Render(DBG_BUFFER_2D* _pBuffer)
{
	// check if it's in view?
	AddLine(_pBuffer, m_From, m_To, m_Color); 
	if (m_Arrow)
	{
		AddLine(_pBuffer, m_To, m_ArrowPoints[0], m_Color); 
		AddLine(_pBuffer, m_To, m_ArrowPoints[1], m_Color); 
		AddLine(_pBuffer, m_To, m_ArrowPoints[2], m_Color); 
		AddLine(_pBuffer, m_To, m_ArrowPoints[3], m_Color); 
	}

}

void Line2D::Setup(const math::Vec2& _From, const math::Vec2& _To, COLOR _Color, bool _Arrow)
{
	m_Color = _Color;
	m_From	= _From;
	m_To	= _To;
	m_Arrow = _Arrow;
	if (m_Arrow)
	{
		// Draw some arrow head ?
		math::Vec2	Delta  = m_To - m_From;
		math::Vec2 Right(Delta.y, -Delta.x);
		float	Length = (m_To - m_From).Magnitude();
				Delta  /= Length;
		math::Vec2	Angle = math::Vec2(0, 1);
		float Dot = (Delta ^ Angle);
		if ( Dot > 0.99f)
		{
			Angle = math::Vec2(1, 0);
		}
	

		m_ArrowPoints[0] = _To;
		m_ArrowPoints[1] = _To-(Delta*Length*0.2f) + (Right * 0.1f);
		m_ArrowPoints[2] = _To;
		m_ArrowPoints[3] = _To-(Delta*Length*0.2f) - (Right * 0.1f);
	}

}
